<template>
  <view class="password-container pageBg">
    <view class="page-header">
      <view class="page-title">弱密码检测应用</view>
      <view class="page-description">
        检测并评估密码安全性，识别常见的弱密码模式，提供安全建议
      </view>
    </view>

    <view class="content-area">
      <!-- 密码输入区域 -->
      <view class="input-section">
        <view class="section-title">
          <uni-icons type="locked" size="20" color="#722ed1"></uni-icons>
          <text>输入需要检测的密码</text>
        </view>
        
        <view class="password-input-area">
          <view class="password-field">
            <input 
              :type="showPassword ? 'text' : 'password'" 
              v-model="password" 
              placeholder="请输入密码..." 
              class="password-input"
              @input="updateStrengthMeter"
            />
            <view class="toggle-visibility" @click="togglePasswordVisibility">
              <uni-icons :type="showPassword ? 'eye-slash' : 'eye'" size="20" color="#666"></uni-icons>
            </view>
          </view>
          
          <!-- 密码强度指示器 -->
          <view class="strength-meter">
            <view class="strength-label">密码强度:</view>
            <view class="strength-bars">
              <view 
                v-for="(level, index) in 4" 
                :key="index"
                class="strength-bar" 
                :class="{ 
                  active: strengthLevel >= level,
                  weak: strengthLevel >= level && strengthLevel === 1,
                  medium: strengthLevel >= level && strengthLevel === 2,
                  strong: strengthLevel >= level && strengthLevel === 3,
                  'very-strong': strengthLevel >= level && strengthLevel === 4
                }"
              ></view>
            </view>
            <view class="strength-text" :class="{
              'weak-text': strengthLevel === 1,
              'medium-text': strengthLevel === 2,
              'strong-text': strengthLevel === 3,
              'very-strong-text': strengthLevel === 4
            }">{{ strengthText }}</view>
          </view>
          
          <view class="action-buttons">
            <button 
              class="analyze-btn" 
              :disabled="!canAnalyze" 
              :class="{ disabled: !canAnalyze }"
              @click="analyzePassword"
            >
              <uni-icons type="search" size="18" color="#fff"></uni-icons>
              <text>检测密码</text>
            </button>
          </view>
        </view>
      </view>
      
      <!-- 分析结果区域 -->
      <view v-if="showResults" class="result-section">
        <view class="section-title">
          <uni-icons type="info-filled" size="20" color="#722ed1"></uni-icons>
          <text>检测结果</text>
        </view>
        
        <view class="result-content">
          <view v-if="isLoading" class="loading-state">
            <uni-icons type="spinner-cycle" size="30" color="#722ed1"></uni-icons>
            <text>正在分析中，请稍候...</text>
          </view>
          <view v-else>
            <!-- 密码分析结果 -->
            <view v-if="analysisResult" class="password-analysis">
              <view v-for="(result, index) in analysisResult.检测结果" :key="index" class="analysis-card">
                <view class="card-header">
                  <view class="password-value">{{ result.密码 }}</view>
                  <view class="risk-level" :class="{
                    'low-risk': result.风险等级 === 'low',
                    'medium-risk': result.风险等级 === 'medium',
                    'high-risk': result.风险等级 === 'high'
                  }">
                    <uni-icons :type="getRiskIcon(result.风险等级)" size="16" :color="getRiskColor(result.风险等级)"></uni-icons>
                    <text>{{ getRiskText(result.风险等级) }}</text>
                  </view>
                </view>
                
                <view class="weak-points">
                  <view class="weak-points-title">弱项分析:</view>
                  <view class="weak-point-tags">
                    <view v-for="(point, pIndex) in result.弱项分析" :key="pIndex" class="weak-point-tag">
                      {{ point }}
                    </view>
                  </view>
                </view>
                
                <view class="improvement">
                  <view class="improvement-title">改进建议:</view>
                  <view class="improvement-content">{{ result.改进建议 }}</view>
                </view>
              </view>
            </view>
            
            <!-- 安全密码示例 -->
            <view v-if="analysisResult && analysisResult.安全密码示例" class="safe-examples">
              <view class="examples-title">
                <uni-icons type="checkmarkempty" size="18" color="#52c41a"></uni-icons>
                <text>安全密码示例</text>
              </view>
              <view class="examples-list">
                <view v-for="(example, index) in analysisResult.安全密码示例" :key="index" class="example-item">
                  <text>{{ example }}</text>
                  <view class="copy-btn" @click="copyText(example)">
                    <uni-icons type="copy" size="14" color="#722ed1"></uni-icons>
                  </view>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
      
      <!-- 密码安全提示 -->
      <view class="tips-section">
        <view class="section-title">
          <uni-icons type="info" size="20" color="#722ed1"></uni-icons>
          <text>密码安全提示</text>
        </view>
        
        <view class="tips-content">
          <view class="tip-item">
            <uni-icons type="checkbox-filled" size="16" color="#52c41a"></uni-icons>
            <text>使用至少12个字符的密码</text>
          </view>
          <view class="tip-item">
            <uni-icons type="checkbox-filled" size="16" color="#52c41a"></uni-icons>
            <text>混合使用大小写字母、数字和特殊符号</text>
          </view>
          <view class="tip-item">
            <uni-icons type="checkbox-filled" size="16" color="#52c41a"></uni-icons>
            <text>避免使用个人信息，如生日、姓名等</text>
          </view>
          <view class="tip-item">
            <uni-icons type="checkbox-filled" size="16" color="#52c41a"></uni-icons>
            <text>不要使用常见单词或简单的字符序列</text>
          </view>
          <view class="tip-item">
            <uni-icons type="checkbox-filled" size="16" color="#52c41a"></uni-icons>
            <text>为不同的账户使用不同的密码</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed, reactive } from 'vue';
import { openai } from '@/api/apis';

// 状态变量
const password = ref('');
const showPassword = ref(false);
const strengthLevel = ref(0);
const strengthText = ref('');
const isLoading = ref(false);
const showResults = ref(false);
const analysisResult = ref(null);

// 计算属性
const canAnalyze = computed(() => {
  return password.value.trim().length > 0;
});

// 切换密码可见性
const togglePasswordVisibility = () => {
  showPassword.value = !showPassword.value;
};

// 更新密码强度指示器
const updateStrengthMeter = () => {
  const pwd = password.value;
  
  if (!pwd) {
    strengthLevel.value = 0;
    strengthText.value = '';
    return;
  }
  
  // 简单的密码强度评估逻辑
  let score = 0;
  
  // 长度检查
  if (pwd.length >= 8) score += 1;
  if (pwd.length >= 12) score += 1;
  
  // 复杂度检查
  if (/[A-Z]/.test(pwd)) score += 1;
  if (/[a-z]/.test(pwd)) score += 1;
  if (/[0-9]/.test(pwd)) score += 1;
  if (/[^A-Za-z0-9]/.test(pwd)) score += 1;
  
  // 设置强度级别 (1-4)
  if (score <= 2) {
    strengthLevel.value = 1;
    strengthText.value = '弱';
  } else if (score <= 4) {
    strengthLevel.value = 2;
    strengthText.value = '中';
  } else if (score <= 5) {
    strengthLevel.value = 3;
    strengthText.value = '强';
  } else {
    strengthLevel.value = 4;
    strengthText.value = '非常强';
  }
};

// 获取风险图标
const getRiskIcon = (level) => {
  switch(level) {
    case 'low': return 'checkmarkempty';
    case 'medium': return 'warning';
    case 'high': return 'closeempty';
    default: return 'info';
  }
};

// 获取风险颜色
const getRiskColor = (level) => {
  switch(level) {
    case 'low': return '#52c41a';
    case 'medium': return '#faad14';
    case 'high': return '#ff4d4f';
    default: return '#666666';
  }
};

// 获取风险文本
const getRiskText = (level) => {
  switch(level) {
    case 'low': return '低风险';
    case 'medium': return '中等风险';
    case 'high': return '高风险';
    default: return '未知风险';
  }
};

// 复制文本
const copyText = (text) => {
  uni.setClipboardData({
    data: text,
    success: () => {
      uni.showToast({
        title: '已复制到剪贴板',
        icon: 'success'
      });
    }
  });
};

// 分析密码
const analyzePassword = async () => {
  if (!canAnalyze.value) return;
  
  isLoading.value = true;
  showResults.value = true;
  
  try {
    // 准备请求数据
    const requestData = {
      content: password.value,
      id: "1895725486034681857" // 弱密码检测应用ID
    };
    
    // 调用API
    const response = await openai(requestData);
    
    if (response.code === 200) {
      // 解析返回的JSON数据
      const content = response.data.content;
      if (content && content.includes('```json')) {
        const jsonMatch = content.match(/```json\n([\s\S]*?)\n```/);
        if (jsonMatch && jsonMatch[1]) {
          analysisResult.value = JSON.parse(jsonMatch[1]);
        }
      } else {
        throw new Error('返回数据格式不正确');
      }
    } else {
      throw new Error(response.message || '请求失败');
    }
  } catch (error) {
    console.error('分析失败:', error);
    uni.showToast({
      title: '分析失败，请重试',
      icon: 'none'
    });
  } finally {
    isLoading.value = false;
  }
};
</script>

<style>
.password-container {
  padding: 30rpx;
  min-height: 100vh;
}

.page-header {
  margin-bottom: 40rpx;
  text-align: center;
  animation: fadeIn 0.8s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-20rpx); }
  to { opacity: 1; transform: translateY(0); }
}

.page-title {
  font-size: 40rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 16rpx;
}

.page-description {
  font-size: 28rpx;
  color: #666;
  max-width: 80%;
  margin: 0 auto;
}

.content-area {
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 16rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
  overflow: hidden;
  backdrop-filter: blur(10rpx);
  animation: slideUp 0.6s ease;
}

@keyframes slideUp {
  from { opacity: 0; transform: translateY(40rpx); }
  to { opacity: 1; transform: translateY(0); }
}

.input-section, .result-section, .tips-section {
  padding: 30rpx;
  border-bottom: 2rpx solid #f0f0f0;
  position: relative;
}

.input-section::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 6rpx;
  height: 100%;
  background: linear-gradient(to bottom, #722ed1, #b37feb);
  border-radius: 3rpx;
}

.result-section::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 6rpx;
  height: 100%;
  background: linear-gradient(to bottom, #722ed1, #b37feb);
  border-radius: 3rpx;
}

.tips-section::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 6rpx;
  height: 100%;
  background: linear-gradient(to bottom, #722ed1, #b37feb);
  border-radius: 3rpx;
}

.section-title {
  display: flex;
  align-items: center;
  font-size: 32rpx;
  font-weight: 500;
  color: #333;
  margin-bottom: 30rpx;
}

.section-title text {
  margin-left: 12rpx;
}

.password-input-area {
  background-color: #f9f0ff;
  border-radius: 12rpx;
  padding: 30rpx;
}

.password-field {
  position: relative;
  margin-bottom: 24rpx;
}

.password-input {
  width: 83%;
  height: 80rpx;
  padding: 0 80rpx 0 20rpx;
  border: 2rpx solid #d3adf7;
  border-radius: 8rpx;
  font-size: 28rpx;
  background-color: #fff;
  transition: all 0.3s;
}

.password-input:focus {
  border-color: #722ed1;
  box-shadow: 0 0 0 2rpx rgba(114, 46, 209, 0.2);
}

.toggle-visibility {
  position: absolute;
  right: 20rpx;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
}

.strength-meter {
  display: flex;
  align-items: center;
  margin-bottom: 30rpx;
}

.strength-label {
  font-size: 26rpx;
  color: #666;
  margin-right: 16rpx;
}

.strength-bars {
  display: flex;
  gap: 6rpx;
  margin-right: 16rpx;
}

.strength-bar {
  width: 40rpx;
  height: 8rpx;
  background-color: #e8e8e8;
  border-radius: 4rpx;
  transition: all 0.3s;
}

.strength-bar.active.weak {
  background-color: #ff4d4f;
}

.strength-bar.active.medium {
  background-color: #faad14;
}

.strength-bar.active.strong {
  background-color: #52c41a;
}

.strength-bar.active.very-strong {
  background-color: #13c2c2;
}

.strength-text {
  font-size: 26rpx;
  font-weight: 500;
}

.weak-text {
  color: #ff4d4f;
}

.medium-text {
  color: #faad14;
}

.strong-text {
  color: #52c41a;
}

.very-strong-text {
  color: #13c2c2;
}

.action-buttons {
  display: flex;
  justify-content: center;
}

.analyze-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 40rpx;
  height: 80rpx;
  background: linear-gradient(90deg, #722ed1 0%, #b37feb 100%);
  color: #fff;
  border-radius: 40rpx;
  font-size: 28rpx;
  font-weight: 500;
  box-shadow: 0 6rpx 16rpx rgba(114, 46, 209, 0.3);
  transition: all 0.3s;
}

.analyze-btn:active {
  transform: translateY(2rpx);
}

.analyze-btn text {
  margin-left: 8rpx;
}

.analyze-btn.disabled {
  background: linear-gradient(90deg, #d9d9d9 0%, #c0c0c0 100%);
  box-shadow: none;
  cursor: not-allowed;
}

.result-content {
  background-color: #f9f0ff;
  border-radius: 12rpx;
  padding: 20rpx;
  min-height: 200rpx;
}

.loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40rpx 0;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-state uni-icons {
  animation: spin 1.5s linear infinite;
}

.loading-state text {
  margin-top: 20rpx;
  font-size: 28rpx;
  color: #666;
}

.password-analysis {
  margin-bottom: 30rpx;
}

.analysis-card {
  background-color: #fff;
  border-radius: 12rpx;
  padding: 24rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
  padding-bottom: 16rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.password-value {
  font-size: 32rpx;
  font-weight: 500;
  color: #333;
  font-family: monospace;
}

.risk-level {
  display: flex;
  align-items: center;
  padding: 4rpx 16rpx;
  border-radius: 30rpx;
  font-size: 24rpx;
}

.risk-level text {
  margin-left: 6rpx;
}

.low-risk {
  background-color: #f6ffed;
  border: 1rpx solid #b7eb8f;
  color: #52c41a;
}

.medium-risk {
  background-color: #fffbe6;
  border: 1rpx solid #ffe58f;
  color: #faad14;
}

.high-risk {
  background-color: #fff1f0;
  border: 1rpx solid #ffa39e;
  color: #ff4d4f;
}

.weak-points {
  margin-bottom: 20rpx;
}

.weak-points-title {
  font-size: 28rpx;
  font-weight: 500;
  color: #333;
  margin-bottom: 12rpx;
}

.weak-point-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 12rpx;
}

.weak-point-tag {
  background-color: #fff1f0;
  color: #ff4d4f;
  padding: 6rpx 16rpx;
  border-radius: 30rpx;
  font-size: 24rpx;
  border: 1rpx solid #ffa39e;
}

.improvement-title {
  font-size: 28rpx;
  font-weight: 500;
  color: #333;
  margin-bottom: 12rpx;
}

.improvement-content {
  font-size: 26rpx;
  color: #666;
  line-height: 1.6;
}

.safe-examples {
  background-color: #f6ffed;
  border-radius: 12rpx;
  padding: 20rpx;
  border: 1rpx solid #b7eb8f;
}

.examples-title {
  display: flex;
  align-items: center;
  font-size: 28rpx;
  font-weight: 500;
  color: #333;
  margin-bottom: 16rpx;
}

.examples-title text {
  margin-left: 8rpx;
}

.examples-list {
  display: flex;
  flex-direction: column;
  gap: 12rpx;
}

.example-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  padding: 12rpx 16rpx;
  border-radius: 8rpx;
  font-family: monospace;
  font-size: 26rpx;
}

.copy-btn {
  padding: 6rpx;
  border-radius: 4rpx;
  cursor: pointer;
}

.copy-btn:active {
  background-color: #f0f0f0;
}

.tips-content {
  background-color: #f9f0ff;
  border-radius: 12rpx;
  padding: 20rpx;
}

.tip-item {
  display: flex;
  align-items: flex-start;
  margin-bottom: 16rpx;
}

.tip-item:last-child {
  margin-bottom: 0;
}

.tip-item uni-icons {
  margin-right: 12rpx;
  margin-top: 4rpx;
}

.tip-item text {
  font-size: 26rpx;
  color: #666;
  line-height: 1.6;
}
</style>